<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    
<div id="app">
    <cpn v-on:item-click="cpnClick"></cpn>
</div>

<template id="cpn">
    <div>
        <button v-for="(item, index) in categories" :key="index" v-on:click="btnClick(item)">
            {{item.name}}
        </button>
    </div>
</template>
<script src="../js/vue.js"></script>
<script>
    // 子组件
    const cpn = {
        template: '#cpn',
        data() {
            return {
                categories: [
                    {id: '001', name: '热门推荐'},
                    {id: '002', name: '手机数码'},
                    {id: '003', name: '家用家电'},
                    {id: '004', name: '电脑办公'}
                ]
            }
        }, 
        methods: {
            btnClick(item){
                // console.log(item);
                // 发射,自定义事件
                this.$emit('item-click', item)   
            }
        },
    }

    // 父组件
    const app = new Vue({
        el: '#app',
        components: {
            cpn
        },
        methods: {
            cpnClick(item){
                console.log('子组件发射成功', item);      
            }
        },
    })
</script>
</body>
</html>